<template>
  <div class="app-container">
    <!--工具栏-->
    <Search :crud="crud">
      <el-form-item label="姓名">
        <el-input v-model="query.roleName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input v-model="query.phone" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="招聘类型">
        <el-select v-model="query.recruitmentType" placeholder="请选择">
          <el-option
            v-for="item in dict.RECRUITMENT_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </Search>
    <el-card>
      <div class="head-container">
        <crudOperation :permission="permission">
          <template slot="right">
            <el-button
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="handle(null, 'new')"
              >新建</el-button
            >
            <!-- <el-button class="filter-item" size="mini" type="primary" icon="el-icon-delete" @click="handle(null,'batchDelete')">批量删除</el-button> -->
          </template>
        </crudOperation>
      </div>
      <el-table
        ref="table"
        border
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column prop="headPicUrl" label="头像" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.headPicUrl"
              :preview-src-list="[scope.row.headPicUrl]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          align="center"
          width="140"
        />
        <el-table-column prop="realName" label="真实姓名" align="center" />
        <el-table-column
          prop="department"
          label="所属部门"
          align="center"
          width="300"
        >
        </el-table-column>
        <el-table-column prop="post" label="岗位" align="center" width="140" />
        <el-table-column prop="education" label="学历" align="center" />
        <el-table-column
          prop="jobPerformance"
          label="岗位绩效"
          align="center"
        />
        <el-table-column
          prop="accumulatedWorkingYears"
          label="累计工年"
          align="center"
        />
        <el-table-column prop="companyYears" label="公司年限" align="center" />
        <el-table-column prop="freeze" label="积分冻结" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.freeze === '1' ? '已冻结' : '未冻结' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="entryTime"
          label="入职时间"
          align="center"
          width="140"
        >
          <template slot-scope="scope">
            <span>{{ new Date(scope.row.entryTime).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="createdByName" label="创建人" align="center" /> -->
        <el-table-column
          prop="createdAt"
          label="创建日期"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ new Date(scope.row.createdAt).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="520" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="handle(scope.row, 'delete')"
              >删除</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="handle(scope.row, 'update')"
              >修改</el-button
            >
            <el-button
              type="success"
              size="mini"
              @click="handle(scope.row, 'role')"
              >角色分配</el-button
            >
            <el-button
              type="warning"
              size="mini"
              @click="handle(scope.row, 'password')"
              >修改密码</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="handle(scope.row, 'bindMall')"
              >绑定店铺</el-button
            >
            <el-button
              :disabled="scope.row.freeze === '1'"
              type="primary"
              size="mini"
              @click="handle(scope.row, 'freezePoints')"
              >冻结积分</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页组件-->
      <pagination />
    </el-card>
    <UserDialog
      ref="handbookDialog"
      @submitSuccess="submitSuccess"
    ></UserDialog>
    <roleDialog
      ref="handRoleDialog"
      :roleList="roleList"
      @submitSuccess="submitSuccess"
    ></roleDialog>
    <changePassword
      ref="passwordDialog"
      @submitSuccess="submitSuccess"
    ></changePassword>
    <bindMallDialog
      ref="bindMallDialogRef"
      :submitSuccess="submitSuccess"
    ></bindMallDialog>
  </div>
</template>

<script>
import crudJob from '@/api/system/job'
import crudUser from '@/api/system/user'
import CRUD, { presenter, header, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import Search from '@/components/Search'

import internalRecommendation from '@/api/inviteJob/internalRecommendation/index'
import UserDialog from '@/views/user/userDialog.vue'
import roleDialog from '@/views/user/roleSelectDialog.vue'
import changePassword from '@/views/user/passwordDialog.vue'
import { showDictValue } from '@/utils'
import bindMallDialog from './bindMallDialog.vue'
export default {
  name: 'BrandIntroductionManage',
  components: {
    crudOperation,
    pagination,
    udOperation,
    Search,
    UserDialog,
    roleDialog,
    changePassword,
    bindMallDialog,
  },
  cruds() {
    return CRUD({
      title: '内部推荐列表',
      url: 'users/list',
      requestMethod: 'get',
      sort: ['jobSort,asc2', 'id,desc'],
      // takingType:'3',
      otherParams: { takingType: '3' },
      crudMethod: { ...crudJob },
    })
  },
  // mixins: [presenter()],
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['RECRUITMENT_TYPE'],
  data() {
    return {
      permission: {
        add: ['admin', 'job:add'],
        edit: ['admin', 'job:edit'],
        del: ['admin', 'job:del'],
      },
      roleList: [],
    }
  },
  mounted() {
    this.getUserRoles()
  },
  methods: {
    showDictValue,
    getUserRoles() {
      crudUser.getRoleSelect().then((res) => {
        this.roleList = res
      })
    },
    // 改变状态
    changeEnabled(data, val) {
      product
        .tbDemoMoldTakingUpdateStatus({ id: data.id, isRelease: val })
        .then(() => {
          this.$notify({ message: '操作成功', type: 'success' })
          this.crud.refresh()
        })
        .catch((err) => {
          data.isRelease = !data.isRelease
          if (data.isRelease == '1') {
            data.isRelease = '0'
          } else {
            data.isRelease = '1'
          }
        })
    },
    // 删除与修改事件
    handle(row, type) {
      if (type == 'delete') {
        //删除
        this.deleteHandle(row)
      } else if (type == 'update') {
        // 修改
        let forms = JSON.parse(JSON.stringify(row))
        this.$refs.handbookDialog.form = {
          //forms
          id: forms.id,
          roleId: forms.roleId, //角色
          name: forms.name,
          phone: forms.phone,
        }
        this.$refs.handbookDialog.isEdit = true
        this.$refs.handbookDialog.dialogVisible = true
      } else if (type == 'new') {
        // 新增
        this.$refs.handbookDialog.form = {
          id: '',
          roleId: '', //角色
          name: '',
          phone: '',
        }
        this.$refs.handbookDialog.isEdit = false
        this.$refs.handbookDialog.dialogVisible = true
      } else if (type == 'batchDelete') {
        // 批量删除
        if (this.crud.selections.length == 0) {
          this.$notify({ title: '请选择需要删除的数据', type: 'warning' })
          return
        }
        const idList = []
        for (let index = 0; index < this.crud.selections.length; index++) {
          const element = this.crud.selections[index]
          idList.push(element.id)
        }
        this.deleteHandle(idList)
      } else if (type === 'role') {
        let forms = JSON.parse(JSON.stringify(row))
        this.$refs.handRoleDialog.form = {
          id: forms.id,
          roleId: forms.type, //角色
        }
        this.$refs.handRoleDialog.isEdit = true
        this.$refs.handRoleDialog.dialogVisible = true
      } else if (type === 'password') {
        let forms = JSON.parse(JSON.stringify(row))
        this.$refs.passwordDialog.form = {
          id: forms.id,
          realName: forms.realName,
          phone: forms.phone,
        }
        this.$refs.passwordDialog.isEdit = true
        this.$refs.passwordDialog.dialogVisible = true
      } else if (type === 'bindMall') {
        this.$refs.bindMallDialogRef.handleOpen(row)
      } else if (type === 'freezePoints') {
        this.freezePointsHandle(row)
      }
    },
    // 冻结积分
    freezePointsHandle(params) {
      this.$confirm('确定冻结该用户积分吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        crudUser
          .freezePoints({ ...params, freeze: '1' })
          .then((res) => {
            this.$notify({ message: '操作成功', type: 'success' })
            this.crud.refresh()
          })
          .catch(() => {})
      })
    },
    // 删除
    deleteHandle(params) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        crudUser
          .deleteHandle([params.id])
          .then((res) => {
            this.$notify({ message: '操作成功', type: 'success' })
            this.crud.refresh()
          })
          .catch(() => {})
      })
    },
    // 新建取模成功
    submitSuccess() {
      this.crud.refresh()
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
.item {
  margin-top: 10px;
  margin-right: 10px;
}
</style>
